package cn.laixueit.service.impl;

import cn.laixueit.mapper.SpuDetailMapper;
import cn.laixueit.mapper.SpuMapper;
import cn.laixueit.mapper.StockMapper;
import cn.laixueit.pojo.*;
import cn.laixueit.result.PageResult;
import cn.laixueit.service.BrandService;
import cn.laixueit.service.CategoryService;
import cn.laixueit.service.SkuService;
import cn.laixueit.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Autowired
    private SpuDetailMapper spuDetailMapper;
    @Autowired
    private SkuService skuService;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageResult<Spu> spuPage(String key, Boolean saleable, Integer page, Integer rows) {
        Page<Spu> p = new Page<>(page, rows);
        IPage<Spu> spuIPage = page(p, getWrapper(key, saleable));
        List<Goods> goodsList = spuIPage.getRecords().stream().map(spu -> {
            Goods goods = new Goods();
            BeanUtils.copyProperties(spu,goods);
            Brand brand = brandService.getById(spu.getBrandId());
            if (brand != null) {
                goods.setBname(brand.getName());
            }
            Category category = categoryService.getById(spu.getCid3());
            if (category != null) {
                goods.setCname(category.getName());
            }
            return goods;
        }).collect(Collectors.toList());
        PageResult<Spu> result = new PageResult<>();
        result.setItems(spuIPage.getRecords());
        result.setTotal(spuIPage.getTotal());
        result.setTotalPage(spuIPage.getPages());
        return result;
    }

    @Override
    @Transactional
    public void saveGoods(Goods goods) {
        Spu spu = goods;
        spu.setSaleable(true);
        spu.setCreateTime(new Date());
        spu.setLastUpdateTime(new Date());
        save(spu);

        SpuDetail spuDetail = goods.getSpuDetail();
        spuDetail.setSpuId(spu.getId());
        spuDetailMapper.insert(spuDetail);

        for (Sku sku : goods.getSkus()) {
            sku.setSpuId(spu.getId());
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(new Date());
            skuService.save(sku);

            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            stock.setStock(sku.getStock());
            stockMapper.insert(stock);
        }
    }

    @Override
    public List<Sku> skuListBySpuId(Long spuId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("spu_id",spuId);
        List<Sku> skuList = skuService.listByMap(map).stream().collect(Collectors.toList());
        return skuList;
    }

    @Override
    public SpuDetail spuDetail(Long id) {
        SpuDetail spuDetail = spuDetailMapper.selectById(id);
        return spuDetail;
    }

    private Wrapper<Spu> getWrapper(String key, Boolean saleable) {
        QueryWrapper<Spu> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(key)){
            wrapper.like("title",key);
        }
        if(saleable != null){
            wrapper.eq("saleable",saleable);
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }
}
